package com.ruoyi.frame.util;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 生成traceId
 *
 * @author zengXiangJiang
 * @version 1.0.0
 */
@Slf4j
public class TraceIdUtil {

    private static final ThreadLocal<String> TRACE_ID = new ThreadLocal<>();

    public static String getTraceId() {
        if (null == TRACE_ID.get()) {
            String s = IdUtil.simpleUUID();
            setTraceId(s);
        }
        return TRACE_ID.get();
    }

    private static void setTraceId(String traceId) {
        TRACE_ID.set(traceId);
    }

    public static void removeTraceId() {
        String traceId = TRACE_ID.get();
        log.trace("traceId: {}", traceId);
        if (null != traceId) {
            TRACE_ID.remove();
        }
    }

}
